/**
 * @description:银行同一账户进行存取款
 * @author:Mr.Cat
 * @create:2021-09-22 19:27
 **/
public class BankThreadTest {
    public static void main(String[] args) {
        Account acct = new Account(1000);
        Coustomer c1 = new Coustomer(acct);
        Coustomer c2 = new Coustomer(acct);

        c1.setName("甲");
        c2.setName("乙");

        c1.start();
        c2.start();
    }
}

class Account {
    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    //存取
    public void deposit(double amt) {
        if (amt > 0) {
            balance += amt;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "：存钱成功。余额为" + balance);
        }
    }
}

class Coustomer extends Thread {
    private Account acct;

    public Coustomer(Account acct) {
        this.acct = acct;
    }

    @Override
    public void run() {
        synchronized (this.getClass()) {
            for (int i = 0; i < 3; i++) {
                acct.deposit(1000);
            }
        }
    }
}